<template>
  <div id="homePage">
    <!-- 搜索框 -->
    <div class="search-bar">
      <a-input-search
        v-model:value="searchParams.searchText"
        placeholder="从海量图片中搜索"
        enter-button="搜索"
        size="large"
        @search="doSearch"
      />
    </div>
    <!-- 分类和标签筛选 -->
    <a-tabs v-model:active-key="selectedCategory" @change="doSearch">
      <a-tab-pane key="all" tab="全部" />
      <a-tab-pane v-for="category in categoryList" :tab="category" :key="category" />
    </a-tabs>
    <div class="tag-bar">
      <span style="margin-right: 8px">标签：</span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectedTagList[index]"
          @change="doSearch"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>
    <!-- 图片列表 -->
    <PictureList :dataList="dataList" :loading="loading" />
    <!-- 分页 -->
    <a-pagination
      style="text-align: right"
      v-model:current="searchParams.current"
      v-model:pageSize="searchParams.pageSize"
      :total="total"
      @change="onPageChange"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost,
} from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import PictureList from '@/components/PictureList.vue' // 定义数据

// 定义数据
const dataList = ref<API.PictureVO[]>([])
const total = ref(0)
const loading = ref(true)

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 12,
  sortField: 'createTime',
  sortOrder: 'descend',
})

// 获取数据
const fetchData = async () => {
  loading.value = true
  // 转换搜索参数
  const params = {
    ...searchParams,
    tags: [] as string[],
  }
  if (selectedCategory.value !== 'all') {
    params.category = selectedCategory.value
  }
  // [true, false, false] => ['java']
  selectedTagList.value.forEach((useTag, index) => {
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })
  const res = await listPictureVoByPageUsingPost(params)
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
  loading.value = false
}

// 页面加载时获取数据，请求一次
onMounted(() => {
  fetchData()
})

// 分页参数
const onPageChange = (page: number, pageSize: number) => {
  searchParams.current = page
  searchParams.pageSize = pageSize
  fetchData()
}

// 搜索
const doSearch = () => {
  // 重置搜索条件
  searchParams.current = 1
  fetchData()
}

// 标签和分类列表
const categoryList = ref<string[]>([])
const selectedCategory = ref<string>('all')
const tagList = ref<string[]>([])
const selectedTagList = ref<boolean[]>([])

/**
 * 获取标签和分类选项
 * @param values
 */
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    tagList.value = res.data.data.tagList ?? []
    categoryList.value = res.data.data.categoryList ?? []
  } else {
    message.error('获取标签分类列表失败，' + res.data.message)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})
</script>

<style scoped>
#homePage {
  margin-bottom: 16px;
}

#homePage .search-bar {
  max-width: 600px;
  margin: 0 auto 32px;
}

#homePage :deep(.ant-input-search) {
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  border: 2px solid rgba(255, 107, 53, 0.1);
  transition: all 0.3s ease;
}

#homePage :deep(.ant-input-search:hover) {
  border-color: rgba(255, 107, 53, 0.3);
  box-shadow: 0 6px 25px rgba(0, 0, 0, 0.15);
}

#homePage :deep(.ant-input-search:focus-within) {
  border-color: #ff6b35;
  box-shadow: 0 6px 25px rgba(255, 107, 53, 0.2);
}

#homePage :deep(.ant-input) {
  border: none;
  background: transparent;
  font-size: 16px;
  padding: 12px 20px;
}

#homePage :deep(.ant-input-search-button) {
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  border: none;
  border-radius: 0 14px 14px 0;
  height: 48px;
  padding: 0 24px;
  font-weight: 600;
  transition: all 0.3s ease;
}

#homePage :deep(.ant-input-search-button:hover) {
  background: linear-gradient(135deg, #e55a2b 0%, #e0851a 100%);
  transform: translateX(2px);
}

#homePage :deep(.ant-tabs) {
  margin-bottom: 24px;
}

#homePage :deep(.ant-tabs-tab) {
  border-radius: 8px;
  margin-right: 8px;
  transition: all 0.3s ease;
  font-weight: 500;
}

#homePage :deep(.ant-tabs-tab:hover) {
  background: rgba(255, 107, 53, 0.1);
  color: #ff6b35;
}

#homePage :deep(.ant-tabs-tab-active) {
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(255, 107, 53, 0.3);
}

#homePage :deep(.ant-tabs-ink-bar) {
  display: none;
}

#homePage .tag-bar {
  margin-bottom: 24px;
  padding: 16px 20px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
  border: 1px solid rgba(255, 107, 53, 0.1);
}

#homePage :deep(.ant-checkable-tag) {
  border-radius: 20px;
  padding: 6px 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

#homePage :deep(.ant-checkable-tag:hover) {
  background: rgba(255, 107, 53, 0.1);
  color: #ff6b35;
  border-color: rgba(255, 107, 53, 0.3);
}

#homePage :deep(.ant-checkable-tag-checked) {
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  color: white;
  border-color: #ff6b35;
  box-shadow: 0 4px 15px rgba(255, 107, 53, 0.3);
}

#homePage :deep(.ant-pagination) {
  margin-top: 32px;
  text-align: center;
}

#homePage :deep(.ant-pagination-item) {
  border-radius: 8px;
  border: 2px solid #f0f0f0;
  transition: all 0.3s ease;
}

#homePage :deep(.ant-pagination-item:hover) {
  border-color: #ff6b35;
  color: #ff6b35;
}

#homePage :deep(.ant-pagination-item-active) {
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  border-color: #ff6b35;
  color: white;
  box-shadow: 0 4px 15px rgba(255, 107, 53, 0.3);
}
</style>
